package com.lzb.compressmaster.activity;

import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.lzb.compressmaster.BaseAc;
import com.lzb.compressmaster.HomeActivity;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.basic.utils.FileP2pUtil;
import com.lzb.compressmaster.databinding.ActivityVideoCompressSucBinding;
import com.lzb.compressmaster.utils.RxUtil;
import com.lzb.compressmaster.utils.VideoCompressor;

import java.text.SimpleDateFormat;

import io.reactivex.rxjava3.core.ObservableEmitter;

public class VideoCompressSucActivity extends BaseAc<ActivityVideoCompressSucBinding> implements View.OnClickListener {

    public static VideoCompressor.CompressResult result;

    @Override
    protected int onCreate() {
        return R.layout.activity_video_compress_suc;
    }

    @Override
    protected void initView() {
        mDataBinding.ivVideoCompressSucBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvSave.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvSave) {//保存相册
            saveVideo();
        }
    }

    @Override
    protected void initData() {
        int progress = (int) ((float) result.getCompressedSize() / result.getOriginalSize() * 100);
        mDataBinding.tvOriginalSize.setText(ConvertUtils.byte2FitMemorySize(result.getOriginalSize(), 2));
        mDataBinding.tvCompressedSize.setText(ConvertUtils.byte2FitMemorySize(result.getCompressedSize(), 2));
        mDataBinding.pbProgress.setProgress(progress);
        Glide.with(mContext).load(result.getOutputPath()).into(mDataBinding.ivImg);
        mDataBinding.tvFileSize.setText(getString(R.string.file_size_text) + ConvertUtils.byte2FitMemorySize(result.getCompressedSize(), 2));
        VideoCompressor.VideoInfo videoInfo = VideoCompressor.getVideoInfo(result.getOutputPath());
        mDataBinding.tvResolution.setText(getString(R.string.resolution_text) + videoInfo.getWidth() + "*" + videoInfo.getHeight());
        mDataBinding.tvDate.setText(getString(R.string.date_text) + TimeUtils.millis2String(FileUtils.getFileLastModified(result.getOutputPath()), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")));
    }

    //保存视频
    private void saveVideo() {
        showDialog(getString(R.string.save_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        FileP2pUtil.copyPrivateVideoToPublic(mContext, result.getOutputPath());
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_suc);
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 1000);
    }

}